package chat7me_1223.client.ui;

import chat7me_1223.client.Client;
import chat7me_1223.client.util.IOUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
import java.io.OutputStream;

public class RegWindow {
    JFrame win=null;
    JPanel contentPane=null;
    JLabel accountLabel=null;
    JTextField accountInput=null;
    JLabel pwdLabel=null;
    JPasswordField pwdInput=null;
    JLabel rePwdLabel=null;
    JPasswordField rePwdInput=null;
    JButton registerBtn=null;
    JButton loginBtn=null;
    InputStream in;
    OutputStream out;
    Client client;
    RegWindow(Client c){
        client=c;
        client.setRegWindow(this);
        win=new JFrame("注册窗口");
        win.setSize(398,337);
        win.setResizable(false);

        contentPane=new JPanel();
        contentPane.setLayout(null);

        accountLabel=new JLabel("账号：");
        accountLabel.setBounds( new Rectangle(15,31,357,47));
        accountLabel.setFont( new Font("Dialog",Font.BOLD,18) );
        contentPane.add(accountLabel);

        accountInput=new JTextField();
        accountInput.setBounds(new Rectangle(110,34,258,35));
        contentPane.add(accountInput);

        pwdLabel=new JLabel("密码");
        pwdLabel.setBounds(new Rectangle(15,94,357,47));
        pwdLabel.setFont(new Font("Dialog",Font.BOLD,18));
        contentPane.add(pwdLabel);

        pwdInput=new JPasswordField();
        pwdInput.setBounds(new Rectangle(110,97,258,30));
        contentPane.add(pwdInput);

        rePwdLabel=new JLabel("密码");
        rePwdLabel.setBounds(new Rectangle(15,162,357,47) );
        rePwdLabel.setFont(new Font("Dialog",Font.BOLD,18));
        contentPane.add(rePwdLabel);


        rePwdInput=new JPasswordField();
        rePwdInput.setBounds(new Rectangle(110,165,258,30));
        contentPane.add(rePwdInput);

        registerBtn=new JButton("注册");
        registerBtn.setBounds(new Rectangle(175,221,70,31));
        registerBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doRegister();
            }
        });
        contentPane.add(registerBtn);


        loginBtn=new JButton("登录");
        loginBtn.setBounds(new Rectangle(280,270,68,20));
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                closeRegWindow();
                LoginWindow loginWindow=new LoginWindow(client);
                client.setLoginWindow(loginWindow);
            }
        });
        contentPane.add(loginBtn);

        win.add(contentPane);
        win.setLocation(410,210);
        win.setVisible(true);

    }

    public void closeRegWindow(){
        win.dispose();
        client.setRegWindow(null);
    }

    private void doRegister() {
        try {
            if (this.client.getSocket() != null) {
                if (accountInput.getText() != null && "".equals(accountInput.getText()) &&
                        pwdInput.getText() != null && "".equals(pwdInput.getText()) &&
                        rePwdInput.getText() != null && "".equals(rePwdInput.getText())) {
                    JOptionPane.showMessageDialog(null, "两次密码输入不一致");
                } else {
                    if (in == null) {
                        in = this.client.getSocket().getInputStream();
                    }
                    if(out==null){
                        out=this.client.getSocket().getOutputStream();
                    }
                    registerBtn.setEnabled(false);
                    loginBtn.setEnabled(false);
                    this.client.setLoginUsername(accountInput.getText());
                    IOUtil.writeShort(Client.REGISTER_REQUEST, out);
                    IOUtil.writeString(accountInput.getText(),out);
                    IOUtil.writeString(pwdInput.getText(),out);
                }
            }else{
                JOptionPane.showMessageDialog(null, "用户名或密码为空");
            }
        }catch (Exception e){

        }
    }



}

